package servlet;

import service.ClassService;
import service.ClassServiceImpl;
import service.StudentService;
import service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "ClassDeleteServlet",urlPatterns = "/classDeleteServlet")
public class ClassDeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码方式，解决中文乱码的问题
        System.out.println("进来啦。。。。");
        response.setContentType("text/html; charset=utf-8");

        // 1.获取前台页面传递过来的学号
        String[] className = request.getParameterValues("className");
        System.out.println("获取到了要删除的班级名称");
        for(String a:className){
            System.out.println(className);
        }
        // 2.通过Service层实现删除操作
        ClassService classService = new ClassServiceImpl();
        int res = classService.classDeleteService(className);
        System.out.println(res);
        // 3.通过输出流向前台页面展示处理结果，然后重新请求一次数据
        PrintWriter writer = response.getWriter();
        // 获取模块名称
        String contextPath = this.getServletContext().getContextPath();
        // 拼接请求路径
        String hrefPath = contextPath + "/classPageQueryServlet";
        if(0 != res) {
            writer.print("<script>alert('删除班级成功！'); location.href='" + hrefPath
                    + "';</script>");
        }else{
            writer.print("<script>alert('删除班级失败！'); location.href='" + hrefPath
                    + "';</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
